<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Hoodoo::Presenters::Object</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../css/github.css" type="text/css" media="screen" />
<script src="../../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>
    <div class="banner">
        <h1>
            <span class="type">Class</span>
            Hoodoo::Presenters::Object
                <span class="parent">&lt;
                    <a href="Field.html">Hoodoo::Presenters::Field</a>
                </span>
        </h1>
        <ul class="files">
            <li><a href="../../../files/lib/hoodoo/presenters/types/object_rb.html">lib/hoodoo/presenters/types/object.rb</a></li>
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
    <div class="description">
      
<p>A JSON <a href="Object.html">Object</a> schema member.</p>

    </div>








    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
        <dt>N</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-c-new">new</a>
              </li>
          </ul>
        </dd>
        <dt>R</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-i-render">render</a>
              </li>
          </ul>
        </dd>
        <dt>V</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-i-validate">validate</a>
              </li>
          </ul>
        </dd>
        <dt>W</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-i-walk">walk</a>
              </li>
          </ul>
        </dd>
    </dl>

    <!-- Includes -->
    <div class="sectiontitle">Included Modules</div>
    <ul>
        <li>
            <a href="BaseDSL.html">
              Hoodoo::Presenters::BaseDSL
            </a>
        </li>
    </ul>







      <!-- Section attributes -->
      <div class="sectiontitle">Attributes</div>
      <table border='0' cellpadding='5'>
          <tr valign='top' id='attribute-i-properties'>
            <td class='attr-rw'>
              [RW]
            </td>
            <td class='attr-name'>properties</td>
            <td class='attr-desc'><p>The properties of this object; a <a href="Hash.html">Hash</a> of
<code>Field</code> instances keyed by field name.</p></td>
          </tr>
      </table>


    <!-- Methods -->
      <div class="sectiontitle">Class Public methods</div>
        <div class="method">
          <div class="title method-title" id="method-c-new">
              <b>new</b>( name = nil, options = {} )
            <a href="../../../classes/Hoodoo/Presenters/Object.html#method-c-new" name="method-c-new" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Initialize an <a href="Object.html">Object</a> instance with the
appropriate name and options.</p>
<dl class="rdoc-list note-list"><dt>name
<dd>
<p>The JSON key.</p>
</dd><dt>options
<dd>
<p>A <code>Hash</code> of options, e.g. :required =&gt; true.</p>
</dd></dl>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-c-new_source')" id="l_method-c-new_source">show</a>
              </p>
              <div id="method-c-new_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/presenters/types/object.rb, line 20</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">initialize</span>( <span class="ruby-identifier">name</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-identifier">options</span> = {} )
  <span class="ruby-keyword">super</span>( <span class="ruby-identifier">name</span>, <span class="ruby-identifier">options</span> )

  <span class="ruby-ivar">@properties</span>        = {}
  <span class="ruby-ivar">@internationalised</span> = <span class="ruby-keyword">false</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
      <div class="sectiontitle">Instance Public methods</div>
        <div class="method">
          <div class="title method-title" id="method-i-render">
              <b>render</b>( data, target )
            <a href="../../../classes/Hoodoo/Presenters/Object.html#method-i-render" name="method-i-render" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Render inbound data into a target hash according to the schema, applying
defaults where defined for fields with no value supplied in the inbound
data.</p>
<dl class="rdoc-list note-list"><dt><code>data</code>
<dd>
<p>Inbound data to render.</p>
</dd><dt><code>target</code>
<dd>
<p>For internal callers only in theory. The target hash into which rendering
should occur. This may then be merged into outer level hashes as part of
nested items defined in the schema.</p>
</dd></dl>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-i-render_source')" id="l_method-i-render_source">show</a>
              </p>
              <div id="method-i-render_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/presenters/types/object.rb, line 70</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">render</span>( <span class="ruby-identifier">data</span>, <span class="ruby-identifier">target</span> )

  <span class="ruby-comment"># In an simple field, e.g. a text field, then whether or not it has</span>
  <span class="ruby-comment"># a default, should someone give a value of &quot;nil&quot; we expect that field</span>
  <span class="ruby-comment"># to be rendered in output with the explicitly provided &quot;nil&quot; value.</span>
  <span class="ruby-comment"># We thus apply the same to objects. A field with an associated object,</span>
  <span class="ruby-comment"># if rendered with an explicit &quot;nil&quot;, renders as just that.</span>
  <span class="ruby-comment">#</span>
  <span class="ruby-keyword">return</span> <span class="ruby-keyword">super</span>( <span class="ruby-keyword">nil</span>, <span class="ruby-identifier">target</span> ) <span class="ruby-keyword">unless</span> <span class="ruby-identifier">data</span>.<span class="ruby-identifier">is_a?</span>( <span class="ruby-operator">::</span><span class="ruby-constant">Hash</span> ) <span class="ruby-comment"># nil or invalid</span>

  <span class="ruby-identifier">have_rendered_something</span> = <span class="ruby-keyword">false</span>

  <span class="ruby-ivar">@properties</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span> <span class="ruby-identifier">name</span>, <span class="ruby-identifier">property</span> <span class="ruby-operator">|</span>
    <span class="ruby-identifier">name</span>    = <span class="ruby-identifier">name</span>.<span class="ruby-identifier">to_s</span>
    <span class="ruby-identifier">has_key</span> = <span class="ruby-identifier">data</span>.<span class="ruby-identifier">has_key?</span>( <span class="ruby-identifier">name</span> )

    <span class="ruby-keyword">next</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">has_key</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">property</span>.<span class="ruby-identifier">has_default?</span>()

    <span class="ruby-identifier">have_rendered_something</span> = <span class="ruby-keyword">true</span>
    <span class="ruby-identifier">property</span>.<span class="ruby-identifier">render</span>( <span class="ruby-identifier">has_key</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">data</span>[ <span class="ruby-identifier">name</span> ] <span class="ruby-operator">:</span> <span class="ruby-identifier">property</span>.<span class="ruby-identifier">default</span>, <span class="ruby-identifier">target</span> )
  <span class="ruby-keyword">end</span>

  <span class="ruby-comment"># If someone passes an empty object for a field and the object schema</span>
  <span class="ruby-comment"># includes no default values, then the above code would render nothing</span>
  <span class="ruby-comment"># as no properties have associated keys in the input data, and none of</span>
  <span class="ruby-comment"># them have default values either. Make sure we render the field for</span>
  <span class="ruby-comment"># this object with the associated empty object given by the input data</span>
  <span class="ruby-comment"># in such cases.</span>
  <span class="ruby-comment">#</span>
  <span class="ruby-keyword">super</span>( {}, <span class="ruby-identifier">target</span> ) <span class="ruby-keyword">unless</span> <span class="ruby-identifier">have_rendered_something</span>

<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
        <div class="method">
          <div class="title method-title" id="method-i-validate">
              <b>validate</b>( data, path = &#39;&#39; )
            <a href="../../../classes/Hoodoo/Presenters/Object.html#method-i-validate" name="method-i-validate" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Check if data is a valid <a href="Object.html">Object</a> and return a <a
href="../Errors.html">Hoodoo::Errors</a> instance with zero (valid) or more
(has validation problems) errors inside.</p>
<dl class="rdoc-list note-list"><dt><code>data</code>
<dd>
<p><a href="../Data.html">Data</a> to check (and check nested properties
therein). Expected to be nil (unless field is required) or a <a
href="Hash.html">Hash</a>.</p>
</dd><dt><code>path</code>
<dd>
<p>For internal callers only in theory - the human-readable nesting path to
this “level”, as an array. Omitted at the top level. In :errors =&gt; {
:foo =&gt; { … } }, validation of “:foo” would be at path “[ :errors ]”.
Validation of the contents of the object at “:foo” would be under “[
:errors, :foo ]”.</p>
</dd></dl>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-i-validate_source')" id="l_method-i-validate_source">show</a>
              </p>
              <div id="method-i-validate_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/presenters/types/object.rb, line 39</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">validate</span>( <span class="ruby-identifier">data</span>, <span class="ruby-identifier">path</span> = <span class="ruby-string">&#39;&#39;</span> )
  <span class="ruby-identifier">errors</span> = <span class="ruby-keyword">super</span>( <span class="ruby-identifier">data</span>, <span class="ruby-identifier">path</span> )
  <span class="ruby-keyword">return</span> <span class="ruby-identifier">errors</span> <span class="ruby-keyword">if</span> <span class="ruby-operator">!</span><span class="ruby-ivar">@required</span> <span class="ruby-keyword">and</span> <span class="ruby-identifier">data</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-comment"># If there are existing errors, we carry on and validate internally too</span>

  <span class="ruby-keyword">if</span> <span class="ruby-operator">!</span> <span class="ruby-identifier">data</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-operator">!</span> <span class="ruby-identifier">data</span>.<span class="ruby-identifier">is_a?</span>( <span class="ruby-operator">::</span><span class="ruby-constant">Hash</span> )
    <span class="ruby-identifier">errors</span>.<span class="ruby-identifier">add_error</span>(
      <span class="ruby-string">&#39;generic.invalid_object&#39;</span>,
      <span class="ruby-value">:message</span>   =<span class="ruby-operator">&gt;</span> <span class="ruby-node">&quot;Field `#{ full_path( path ) }` is an invalid object&quot;</span>,
      <span class="ruby-value">:reference</span> =<span class="ruby-operator">&gt;</span> { <span class="ruby-value">:field_name</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">full_path</span>( <span class="ruby-identifier">path</span> ) }
    )
  <span class="ruby-keyword">end</span>

  <span class="ruby-ivar">@properties</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span> <span class="ruby-identifier">name</span>, <span class="ruby-identifier">property</span> <span class="ruby-operator">|</span>
    <span class="ruby-identifier">rdata</span> = ( <span class="ruby-identifier">data</span>.<span class="ruby-identifier">is_a?</span>( <span class="ruby-operator">::</span><span class="ruby-constant">Hash</span> ) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">data</span>.<span class="ruby-identifier">has_key?</span>( <span class="ruby-identifier">name</span> ) ) <span class="ruby-operator">?</span> <span class="ruby-identifier">data</span>[ <span class="ruby-identifier">name</span> ] <span class="ruby-operator">:</span> <span class="ruby-keyword">nil</span>
    <span class="ruby-identifier">errors</span>.<span class="ruby-identifier">merge!</span>( <span class="ruby-identifier">property</span>.<span class="ruby-identifier">validate</span>( <span class="ruby-identifier">rdata</span>, <span class="ruby-identifier">full_path</span>( <span class="ruby-identifier">path</span> ) ) )
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">errors</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
        <div class="method">
          <div class="title method-title" id="method-i-walk">
              <b>walk</b>( &amp;block )
            <a href="../../../classes/Hoodoo/Presenters/Object.html#method-i-walk" name="method-i-walk" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Invoke a given block, passing this item; call recursively for any defined
sub-fields too. See Hoodoo::Presenters::Base#walk for why.</p>
<dl class="rdoc-list note-list"><dt>&amp;block
<dd>
<p>Mandatory block, which is passed &#39;self&#39; when called.</p>
</dd></dl>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-i-walk_source')" id="l_method-i-walk_source">show</a>
              </p>
              <div id="method-i-walk_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/presenters/types/object.rb, line 108</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">walk</span>( <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span> )
  <span class="ruby-identifier">block</span>.<span class="ruby-identifier">call</span>( <span class="ruby-keyword">self</span> )

  <span class="ruby-ivar">@properties</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span> <span class="ruby-identifier">name</span>, <span class="ruby-identifier">property</span> <span class="ruby-operator">|</span>
    <span class="ruby-identifier">property</span>.<span class="ruby-identifier">walk</span>( <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span> )
  <span class="ruby-keyword">end</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@properties</span>.<span class="ruby-identifier">nil?</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
</div>

    </div>
  </body>
</html>
